package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.plan.PlanFinishDto;
import com.example.supplychainbackend.controller.dto.plan.PlanOrderDetailDto;
import com.example.supplychainbackend.controller.dto.plan.PlanOrderDto;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.PlanService;
import com.example.supplychainbackend.tool.JwtUtil;
import jakarta.transaction.Transactional;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/plan")
@AllArgsConstructor
public class PlanController {
    private final PlanService planService;

    @PostMapping("/workorder/{id}/chain")
    public DataResponse<UploadInfo> uploadPlanOrder(@PathVariable String id){
        UploadInfo res = planService.uploadPlanOrder(id);
        return DataResponse.success(res);
    }
    @PostMapping("/workorder")
    public DataResponse<String> createPlanOrder(@RequestHeader("Authorization") String authHeader,
                                                @RequestBody PlanOrderDetailDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        String lineNumber = planService.createPlanOrder(form);
        return DataResponse.success(lineNumber);
    }

    @PutMapping("/workorder/{id}")
    public DataResponse<PlanOrderDetailDto> updatePlanOrder(@RequestHeader("Authorization") String authHeader,
                                                            @PathVariable String id, @RequestBody PlanOrderDetailDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        PlanOrderDetailDto res = planService.updatePlanOrder(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/workorder/list")
    public DataResponse<List<PlanOrderDto>> listPlanOrders(@RequestHeader("Authorization") String authHeader) {
        List<PlanOrderDto> res = planService.getPlanOrders();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = planService.getPlanOrdersByEid(eid);
        }
        return DataResponse.success(res);
    }

    @GetMapping("/workorder/{id}")
    public DataResponse<PlanOrderDetailDto> getPlanOrderDetail(@PathVariable String id) {
        PlanOrderDetailDto res = planService.getPlanOrderDetail(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/workorder/{id}")
    @Transactional
    public DataResponse<Long> deletePlanOrder(@PathVariable String id) {
        return DataResponse.success(planService.deletePlanOrder(id));
    }

    @PutMapping("/finish/{id}")
    public DataResponse<PlanOrderDetailDto> finishPlanOrder(@PathVariable String id,
                                                            @RequestBody PlanFinishDto form) {
        PlanOrderDetailDto res = planService.finishPlanOrder(form.getWorkOrderNo(), form.getCompletionStatus());
        return DataResponse.success(res);
    }

    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> planErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }

}
